package group

import (
	"api/common"
	"api/pb/group"
	"context"
	"errors"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateApplyLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateApplyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateApplyLogic {
	return &UpdateApplyLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// UpdateApply 更新入群申请
func (l *UpdateApplyLogic) UpdateApply(req *types.GroupApplyInfo) (resp *types.Empty, err error) {
	// 1. 获取用户标识
	userID, err := common.GetUserIDFromCtx(l.ctx)
	if err != nil {
		return nil, err
	}
	// 2. 校验参数
	if req.ID <= 0 {
		return nil, errors.New("apply_id is required")
	}
	// 3. 更新申请
	_, err = l.svcCtx.GroupRPC.HandleApply(l.ctx, &group.ApplyInfo{Id: req.ID, HandlerUserId: userID, Status: req.Status})
	if err != nil {
		return nil, err
	}
	// 4. 返回结果
	return &types.Empty{}, nil
}
